﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using Telerik.Web.UI;
using Telerik.Cms;
using Telerik.Cms.Web;

namespace SitefinityWatch.Web.UI.Editors
{
    public class PageIdSelector : Telerik.Cms.Web.UI.WebUITypeEditor<Guid>
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.manager = new CmsManager();
            cmsSiteMap = (CmsSiteMapProvider)System.Web.SiteMap.Provider; 
        }

        protected override void CreateChildControls()
        {
            parentPages = new List<Guid>();
            if (Value != Guid.Empty)
            {
                SiteMapNode selectedNode = cmsSiteMap.FindSiteMapNodeFromKey(Value.ToString());
                parentPages = GetParents(selectedNode.ParentNode, parentPages);
                parentPages.Reverse();
            }

            this.Controls.Add(new LiteralControl("<div class=\"sitemapTree\">"));

            RadTreeNode root = new RadTreeNode();
            root.Text = "All Pages";
            root.Value = Guid.Empty.ToString();
            root.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
            root.Expanded = true;
            if (Value == Guid.Empty)
            {
                root.Selected = true;
            }
            
            tree = new RadTreeView();
            tree.NodeExpand += new RadTreeViewEventHandler(tree_NodeExpand);
            tree.Nodes.Add(root);
            this.Controls.Add(tree);
            this.LoadNodes(root);

            this.Controls.Add(tree);

            this.Controls.Add(new LiteralControl("</div>"));
        }

        public void PreselectValue()
        {
            foreach (Guid pageID in parentPages)
            {
                RadTreeNode node = tree.FindNodeByValue(pageID.ToString());
                node.Expanded = true;
                if (node.Nodes.Count == 0)
                {
                    LoadNodes(node);
                }
            }
        }

        private void LoadNodes(RadTreeNode root)
        {
            foreach (ICmsPage page in manager.GetPages(new Guid(root.Value)))
            {
                RadTreeNode node = new RadTreeNode();
                node.Value = page.ID.ToString();
                node.Text = page.MenuName;
                node.ToolTip = page.MenuName;

                if (page.Pages.Count > 0)
                {
                    node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
                }

                if (page.ID == Value)
                {
                    node.Selected = true;
                }

                node.Expanded = true;

                if (parentPages.Contains(page.ID))
                {
                    LoadNodes(node);
                }

                root.Nodes.Add(node);
            }

        }

        private List<Guid> GetParents(SiteMapNode node, List<Guid> Parents)
        {
            if (node != node.RootNode)
            {
                var cmsNode = (CmsSiteMapNode)node;
                Parents.Add(cmsNode.PageID);
                Parents = GetParents(node.ParentNode, Parents);
            }
            return Parents;
        }

        void tree_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            this.LoadNodes(e.Node);
        }

        public override Guid Value
        {
            get
            {
                try
                {
                    return new Guid(tree.SelectedValue);

                }
                catch
                {
                    return _value;
                }
            }
            set
            {
                _value = value;
            }
        }

        private CmsManager manager;
        private CmsSiteMapProvider cmsSiteMap;
        private Guid _value = Guid.Empty;
        private RadTreeView tree;
        private List<Guid> parentPages;
    }
}
